package com.code.lightning.generator;

import com.code.lightning.common.domain.BasicClassConvertMetaData;
import com.code.lightning.common.exception.LightingGeneratorException;
import com.code.lightning.utils.FileUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LGX_TvT
 * @date 2021-01-19 23:47
 */
@Slf4j
public class LightingCodeTemplateGenerator {

    /**
     * 根据元数据以及模板与配置生成实体类
     * @param classConvertMetaData class元数据
     * @param model model
     * @param templateFilePath 模板路径
     * @param outputPath 生成路径
     */
    public void fileTemplateGenerator(List<BasicClassConvertMetaData> classConvertMetaData, Map model, String templateFilePath, String outputPath) throws LightingGeneratorException {
        File sourceFile = new File(templateFilePath);
        if (!sourceFile.exists()) {
            log.error("template file: {} not found.", sourceFile.getName());
            throw new LightingGeneratorException("template file: " + sourceFile.getName() + " not found.");
        }
        BasicTemplateGenerator generator = new BasicTemplateGenerator();
        if (CollectionUtils.isNotEmpty(classConvertMetaData)) {
            for (BasicClassConvertMetaData metaData : classConvertMetaData) {
                model.put("metaData", metaData);
                String targetFileName = generator.generatorContent(sourceFile.getName(), model);
                FileUtil.createDir(outputPath);
                File targetFile = new File(outputPath + "/" + targetFileName);
                generator.generatorContent(sourceFile, targetFile, model);
                log.info("Generator {} success.", targetFileName);
            }
        }
    }

    /**
     * 根据元数据以及字符串模板与配置生成实体
     * @param classConvertMetaData 元数据
     * @param model key model
     * @param template 模板
     * @param templateFileName 模板文件名称
     * @return List<GeneratorEntity>
     */
    public List<GeneratorEntity> stringTemplateGenerator(List<BasicClassConvertMetaData> classConvertMetaData, Map model, String template, String templateFileName) throws LightingGeneratorException {
        List<GeneratorEntity> result = new ArrayList<>();
        BasicTemplateGenerator generator = new BasicTemplateGenerator();
        if (CollectionUtils.isNotEmpty(classConvertMetaData)) {
            for (BasicClassConvertMetaData metaData : classConvertMetaData) {
                model.put("metaData", metaData);
                String targetFileFullName = generator.generatorContent(templateFileName, model);
                String targetFileName = targetFileFullName.split("\\.")[0];
                String content = generator.generatorContent(template, model);
                result.add(new GeneratorEntity(targetFileName, targetFileFullName, content));
                log.info("Generator {} success.", targetFileFullName);
            }
        }
        return result;
    }

    /**
     * 内容实体
     */
    @Data
    public static class GeneratorEntity {

        private String fileName;        // 文件名称
        private String fileFullName;    // 文件全名
        private String fileContent;     // 文件内容

        public GeneratorEntity() {
        }

        public GeneratorEntity(String fileName, String fileFullName, String fileContent) {
            this.fileName = fileName;
            this.fileFullName = fileFullName;
            this.fileContent = fileContent;
        }
    }
}
